package com.yr.ryjk.app;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.app.service.IAppService;
import com.yr.ryjk.common.Constant;
import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.sysuser.entity.SysUser;
import com.yr.ryjk.util.JSONUtil;
/**
 * app更新
 * @author Rick
 *
 */
@Controller
@RequestMapping("/app")
public class AppController {
	@Autowired
	private IAppService appService;
	/**
	 * 后台更新app接口
	 * @param version
	 * @param url
	 * @param status
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(
			@RequestParam("version") @NotBlank String version,
			@RequestParam("url") @NotBlank String url,
			@RequestParam(value="status",defaultValue="1") int status,
			@RequestParam(value="forceUpdate",defaultValue="0") int forceUpdate,
			HttpSession session){
		SysUser su=(SysUser)session.getAttribute(Constant.su_in_session);
		return  appService.add(version,url,status,su.getSuName(),forceUpdate);
	} 
	
	/**
	 * 后台页面分页获取数据接口
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam(value="version",required=false) String version){
		return JSONUtil.getJSON(ErrorType.SUCCESS,appService.getListOfPage(pageNo,pageSize,version));
	}
	
	/**
	 * 设置为默认
	 * @param id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/setOn",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> setDefault(@RequestParam("id") @NotBlank String id){
		appService.updateSetDefault(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 获取最新app,不拦截
	 * @return
	 */
	@RequestMapping(value="/getLatestVersion",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getLatestApp(){
		return JSONUtil.getJSON(ErrorType.SUCCESS,appService.getLatestApp());
	}
}